package com.yuyou.fn.educational.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.Measurement;
import com.yuyou.fn.educational.entity.StudentMeasureScore;
import com.yuyou.fn.educational.mapper.StudentMeasureScoreMapper;
import com.yuyou.fn.educational.service.IMeasurementService;
import com.yuyou.fn.educational.service.IStudentMeasureScoreService;
import com.yuyou.fn.educational.vo.MeasurementQueryVo;
import com.yuyou.fn.educational.vo.StudentMeasureScoreDetailVo;
import com.yuyou.fn.educational.vo.StudentMeasureScoreQueryVo;
import com.yuyou.fn.educational.vo.StudentMeasureScoreSaveOrUpdateVo;
import com.yuyou.fn.platform.entity.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * StudentMeasureScore 表数据服务层接口实现类
 *
 */
@Service
public class StudentMeasureScoreServiceImpl extends SuperServiceImpl<StudentMeasureScoreMapper, StudentMeasureScore,Long> implements IStudentMeasureScoreService {

    @Resource
    private IMeasurementService measurementService;

    @Override
    public StudentMeasureScore saveOrUpdate(StudentMeasureScoreSaveOrUpdateVo studentMeasureScoreSaveOrUpdateVo, SysUser sysUser) {
        if (studentMeasureScoreSaveOrUpdateVo.getWay()==null){
            throw new BusException("来源必须说明");
        }
        StudentMeasureScore studentMeasureScore=new StudentMeasureScore();
        BeanUtils.copyProperties(studentMeasureScore,studentMeasureScoreSaveOrUpdateVo);
        studentMeasureScore.setUpdateTime(TimeUtils.now());
        if (studentMeasureScoreSaveOrUpdateVo.getWay()==0){
            studentMeasureScore.setOperatorId(sysUser.getUserId());
            studentMeasureScore.setOperator(sysUser.getRealName());
        }else {
            //studentMeasureScore.setOperatorId(sysUser.getUserId());
            studentMeasureScore.setOperator("系统");
        }
        if (studentMeasureScore.getStudentMeasureScoreId()!=null){
            this.updateSelectiveById(studentMeasureScore);
        }else {
            if (studentMeasureScoreSaveOrUpdateVo.getWay()==0){
                studentMeasureScore.setCreatorId(sysUser.getUserId());
                studentMeasureScore.setCreator(sysUser.getRealName());
            }else {
                studentMeasureScore.setCreator("系统");
            }
            studentMeasureScore.setCreateTime(TimeUtils.now());
            this.insert(studentMeasureScore);
        }
        return studentMeasureScore;
    }

    @Override
    public List<StudentMeasureScore> findStudentScoreByQuery(StudentMeasureScoreQueryVo studentMeasureScoreQueryVo) {
        Example<StudentMeasureScore> example=StudentMeasureScore.createExample();
        Example.Criteria criteria=example.createCriteria();
        CriteriaUtils.fillCondition(criteria,studentMeasureScoreQueryVo);
        return this.findListByExample(example);
    }

    @Override
    public List<StudentMeasureScoreDetailVo> findStudentScoreDetailByQuery(StudentMeasureScoreQueryVo studentMeasureScoreQueryVo) {
        List<StudentMeasureScoreDetailVo> studentMeasureScoreDetailVoList=new ArrayList<>();
        List<StudentMeasureScore> studentMeasureScoreList = this.findStudentScoreByQuery(studentMeasureScoreQueryVo);
        Map<Integer,String> map=new HashMap<>();
        map.put(2,"清北");
        map.put(3,"状元");
        map.put(4,"尖子");
        if(studentMeasureScoreList!=null){
            for (StudentMeasureScore studentMeasureScore:studentMeasureScoreList){
                StudentMeasureScoreDetailVo studentMeasureScoreDetailVo=new StudentMeasureScoreDetailVo();
                BeanUtils.copyProperties(studentMeasureScoreDetailVo,studentMeasureScore);
                MeasurementQueryVo measurementQueryVo=new MeasurementQueryVo();
                BeanUtils.copyProperties(measurementQueryVo,studentMeasureScore);
                measurementQueryVo.setCampusIds(studentMeasureScore.getCampusId().toString());
                Measurement measurement=measurementService.findMinLevelByStudentScore(measurementQueryVo,studentMeasureScore.getStudentScore());
                if (measurement!=null){
                    studentMeasureScoreDetailVo.setCanRegLevel(map.get(measurement.getLevel())==null?"":map.get(measurement.getLevel()));
                }
                studentMeasureScoreDetailVoList.add(studentMeasureScoreDetailVo);
            }
        }
        return studentMeasureScoreDetailVoList;
    }



    @Override
    public boolean checkPass(Long studentId , Long gradeId, Long subjectId, Integer level, String campusIds, Double passingScore) {
        Example<StudentMeasureScore> example = StudentMeasureScore.createExample();
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("studentId",studentId);
        criteria.andEqualTo("gradeId",gradeId);
        criteria.andEqualTo("subjectId",subjectId);
        if (StringUtils.isNotEmpty(campusIds)){
            criteria.andIn("campusId", Utils.toLongIds(campusIds));
        }
        criteria.andEqualTo("validity",1);
        example.orderBy("studentScore").desc();

        StudentMeasureScore studentMeasureScore = findOneByExample(example);

        return studentMeasureScore == null  ? false : (int) ( studentMeasureScore.getStudentScore() * 10 ) >= (int)(passingScore * 10 ) ;
    }
}